/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threeds.DAO;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Eduardo
 */
public class Banco {
    private Connection con;
    private Statement stm;
    private ResultSet rs;
    private String DRIVER = "org.hsqldb.jdbcDriver";
    //private String URL = "jdbc:hsqldb:file:C:/servers/base_hsqldb";
    //private String URL = "jdbc:hsqldb:file:banco/sebo";
    //private String URL = "jdbc:hsqldb:file:D:/UnB/2ºSemestre(170809 - 1209)/PDS/sistema/testes/banco/b";
    //private String URL = "jdbc:hsqldb:file:D:/hsqldb/banco";
    private String URL = "jdbc:hsqldb:file:"+ new threeds.Validacao().SEPARADOR +"banco/bd"; //VER AQUI
    private String usuario = "sa";
    private String senha = "";
    
    public Banco(){
        
    }
    
    public void abrirConexao(){
        try{
		    Class.forName( getDriver() );

            setCon( DriverManager.getConnection( getUrl(), getUsuario(), getSenha() ) );

            System.out.println("Conectou!");
		}
        catch(ClassNotFoundException e){
		    System.out.println("Erro ao carregar o driver JDBC. ");
		}
        catch(SQLException e){
		    System.out.println("Erro de SQL: " + e);
		    e.printStackTrace();
		}
    }

    public void consultar(String sql){
        try{
            setStm( getCon().createStatement() );
            getStm().execute(sql);
            setRs( getStm().getResultSet() );
        }
        catch(SQLException e){
            System.out.println("Erro na consulta! " + e);
        }
    }

    public void inserir(String sql){
        try{
            setStm( getCon().createStatement() );
            getStm().execute(sql);
        }
        catch( SQLException e ){
            System.out.println("Erro na inserção! " + e);
            //java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10035 table: PROJETISTA
            //tratar qnd já existir chave primaria
        
        }
//TODO Codigo Duplicado
    }

     public void executar(String sql){
        try{
            setStm( getCon().createStatement() );
            getStm().execute(sql);
        }
        catch( SQLException e ){
            System.out.println("Erro na execução! " + e);
        }
    }

    public void fecharConexao(){
       try{
            getStm().execute("SHUTDOWN");
            System.out.println("Fechou!");
        }
        catch( SQLException e ){
            System.out.println("Erro no fechamento da conexao! " + e);
        }
    }

    /**
     * @return the con
     */
    public Connection getCon() {
        return con;
    }

    public void setCon(Connection c){
        this.con = c;
    }

    /**
     * @return the stm
     */
    public Statement getStm() {
        return this.stm;
    }

    public void setStm(Statement s){
        this.stm = s;
    }

    /**
     * @return the rs
     */
    public ResultSet getRs() {
        return this.rs;
    }

    public void setRs(ResultSet rs){
        this.rs = rs;
    }

    /**
     * @return the DRIVER
     */
    public String getDriver() {
        return this.DRIVER;
    }

    /**
     * @return the URL
     */
    public String getUrl() {
        return this.URL;
    }

    public String getUsuario(){
        return this.usuario;
    }

    public String getSenha(){
        return this.senha;
    }

}